package src.com.test.oneDay;

import java.util.*;

public class demo5 {

    List<List<Integer>> res = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    int sum = 0;

    public List<List<Integer>> combinationSum(int[] candidates, int target) {

        Arrays.sort(candidates);
        tracebacking(candidates,0,target);
        return res;
    }

    public void tracebacking(int[] candidates,int index,int target) {

        if(sum == target) {
            res.add(new ArrayList<>(path));
            return;
        }

        for(int i = index;i < candidates.length;i++){

            if(sum + candidates[i] > target) break;

            path.add(candidates[i]);
            sum += candidates[i];
            tracebacking(candidates,i,target);
            path.removeLast();
            sum -= candidates[i];
        }






    }
}
